package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.ProMagnificationPointEntity;
import com.dragon.modules.yuegou.mapper.member.ProMagnificationPointMapper;
import com.dragon.modules.yuegou.model.member.ProMagnificationPoint;
import com.dragon.modules.yuegou.service.member.ProMagnificationPointService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 商品倍率积分/多倍积分表 服务实现
 *
 * @author pbuilder generated
 * @date 2021-12-15 09:38:01
 * @version 2.0
 */
@Service("memProMagnificationPointService")
public class ProMagnificationPointServiceImpl
		extends ServiceImpl<ProMagnificationPointMapper, ProMagnificationPointEntity>
		implements ProMagnificationPointService {

	@Override
	@Transactional
	public void saveProMagnificationPoint(ProMagnificationPoint proMagnificationPoint) {
		if(StringUtils.isNotEmpty(proMagnificationPoint.getId())){
			ProMagnificationPointEntity entity = getById(proMagnificationPoint.getId());
			proMagnificationPoint.setBusdId(entity.getBusdId());
			proMagnificationPoint.setCreatedBy(entity.getCreatedBy());
			proMagnificationPoint.setDel(entity.getDel());
			proMagnificationPoint.setCreatedTime(entity.getCreatedTime());
			proMagnificationPoint.setEnableStatus(entity.getEnableStatus());
			proMagnificationPoint.setModifiedTime(LocalDateTime.now());
			baseMapper.updateById(proMagnificationPoint);

			if (proMagnificationPoint.getRatio().length() == 1 && proMagnificationPoint.getRatio().indexOf("0") >-1){
				baseMapper.updateRatioNull(proMagnificationPoint.getId());
			}
			if (proMagnificationPoint.getRatio().length() == 1 && proMagnificationPoint.getRatio().indexOf("1") >-1){
				baseMapper.updateOnlineRatioNull(proMagnificationPoint.getId());
			}
		}else{
			proMagnificationPoint.setCreatedTime(LocalDateTime.now());
			save(proMagnificationPoint);
		}
	}

	@Override
	public void updateStatus(ProMagnificationPoint proMagnificationPoint) {
		ProMagnificationPointEntity magnificationPoint = getById(proMagnificationPoint.getId());
		magnificationPoint.setEnableStatus(proMagnificationPoint.getEnableStatus());
		baseMapper.updateById(proMagnificationPoint);
	}
}